


var getTitelorFormat = function (str, obj) {

    var ar = str.split('.');

    var fileformat = ar[ar.length - 1];

    ar.splice(ar.length - 1, 1);

    obj.title = ar.join('.');

    obj.fileformat = fileformat;

    return obj;
};
module.exports = function (UC, source, config, ArFor, cache, package) {
    var UserControl = UC;

    var imageMagick = config.imageMagick;


    var UploadFiles = config.UploadFiles;

    var PluginImage = function (req, Callback) {

        var setvalue = function (List) {
            var val = {
                error: 0,
                List: []
            };
            for (var i = 0; i < List.length; i++) {
                val.List.push(List[i])
            }
            Callback(val)
        }
        UploadFiles(req, UserControl, config, function (obj) {
            var val = {
                error: 1
            };
            if (obj.error == 0) {
                if (obj.fileinfo.toCropImgW) {
                    var fileinfo = obj.fileinfo;
                    var toCropImgX = parseInt(fileinfo.toCropImgX[0]);

                    var toCropImgY = parseInt(fileinfo.toCropImgY[0]);

                    var toCropImgW = parseInt(fileinfo.toCropImgW[0]);

                    var toCropImgH = parseInt(fileinfo.toCropImgH[0]);
                    new ArFor(obj.List, function () {
                        setvalue(obj.List)
                    }).forEach(function (item, next) {
                        imageMagick(item.path).crop(toCropImgW, toCropImgH, toCropImgX, toCropImgY).write(item.path, function (err) {
                            next();
                        })
                    })
                } else {

                    setvalue(obj.List)
                }

            } else {
                Callback(val)
            }
        })

    };
    var router = function (req, res, next) {

        var FileType = 1;
        var query = req.query;



        if (query.FileType == "3" || query.FileType == "2" || query.FileType == "1") {
            PluginImage(req, function (value) {
                if (value.List) {
                    value.links = []
                    for (var i = 0; i < value.List.length; i++) {
                        value.id = value.List[i];
                        value.link = '~/Service/Images/' + value.id + '/^200/j.png';
                        value.links.push(value.link);
                    }
                }
                res.Json(value);
            })
        } else {
            var value = {
                error: 1
            };
            res.Json(value);
        }
    }
    return router;
}